import pyodbc
from Code.XmlDataOperation import XmlDataOperation


class XmlDataOperationMDB(XmlDataOperation):
    def __init__(self):
        super().__init__()

    def handle_file(self, path):
        # 文件路径及文件名
        #mdb_file = r'D:\userTme\test.mdb'
        driver = '{Microsoft Access Driver (*.mdb, *.accdb)}'
        cnxn = pyodbc.connect(f'Driver={driver};DBQ={path}')
        crsr = cnxn.cursor()
        dataDic = {}
        # 打印mdb文件中的表名
        #print("mdb的表名：")
        for table_name in crsr.tables(tableType='TABLE'):
            #print(table_name.table_name)
            crsr.execute(f"select Label1 from {table_name.table_name}")
            rows = crsr.fetchall()
            for i in range(0, len(rows)):
                className = rows[i][0]
                if className not in dataDic and len(className) > 0:
                    dataDic[className] = 1
                else:
                    dataDic[className] = dataDic[className] + 1
        return dataDic